// ActionScript file

package util
{
	import mx.messaging.messages.HTTPRequestMessage;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	public class Rest
	{

		private static var instance : Rest;

		public static function getInstance() : Rest {
			if( instance == null )
				instance = new Rest();
			return instance;
		}

		public function doRestCall( url : String, resultFunction : Function, faultFunction : Function = null, restMethod : String = "GET", parms : Object = null ) : void {
			var httpService : HTTPService = new HTTPService( );
			if ( restMethod.toUpperCase() != "GET" )
			{
				httpService.method = HTTPRequestMessage.POST_METHOD;
			
				if( parms == null )
				{
					parms = new Object();
				}
				parms._method = restMethod;
			}
			else
			{
				httpService.method = HTTPRequestMessage.GET_METHOD;
			}

			if( url.indexOf( "http://" ) == -1 ){
				httpService.url = "http://localhost:8080/flexongrails/" + url;
			} else {
				httpService.url = url;
			}
			
			httpService.resultFormat = "e4x";
			httpService.addEventListener( ResultEvent.RESULT, resultFunction );
			if( faultFunction != null )
			{
				httpService.addEventListener( FaultEvent.FAULT, faultFunction );
			}
			httpService.send( parms );
		}
	}
}